<template>
    <component :is="config.layout.layoutMode"></component>
</template>

<script setup lang="ts">
import { useConfig } from '/@/stores/config'
import { useNavTabs } from '/@/stores/navTabs'
import { useSiteConfig } from '/@/stores/siteConfig'
import { useHeight } from '/@/stores/height'
import { useRoute } from 'vue-router'
import Default from '/@/layouts/container/default.vue'
import Classic from '/@/layouts/container/classic.vue'
import Streamline from '/@/layouts/container/streamline.vue'
import { onMounted, onBeforeMount, onUnmounted } from 'vue'
import { Session } from '/@/utils/storage'
import { handleAdminRoute, getFirstRoute, routePush } from '/@/utils/router'
import router from '/@/router/index'
import { adminBaseRoute } from '/@/router/static'
import { BEFORE_RESIZE_LAYOUT } from '/@/stores/constant/cacheKey'
import { Local } from '/@/utils/storage'
import menus from '/@/router/menu'

const navTabs = useNavTabs()
const config = useConfig()
const route = useRoute()
const siteConfig = useSiteConfig()
const height = useHeight()

const getMainHeight = () => {
    const elMain:HTMLDivElement = document.getElementsByClassName('el-main')[0] as HTMLDivElement
    height.setMainHeight(elMain.offsetHeight)
}

onMounted(() => {
    if (!Local.get('token')) return router.push({ name: 'login' })
    init()
    onSetNavTabsMinWidth()   
    getMainHeight()
    window.addEventListener('resize', getMainHeight)
    window.addEventListener('resize', onSetNavTabsMinWidth)
})
onBeforeMount(() => {
    onAdaptiveLayout()
    window.addEventListener('resize', onAdaptiveLayout)
})
onUnmounted(() => {
    window.removeEventListener('resize', onAdaptiveLayout)
    window.removeEventListener('resize', onSetNavTabsMinWidth)
    window.removeEventListener('resize', getMainHeight)
})

const init = () => {
    siteConfig.$state = {
        site_name:'SnBlogAdmin',
        version:'v1.0.0',
        cdn_url:''
    }
    let menuRule = handleAdminRoute(menus)
    // 更新stores中的路由菜单数据
    navTabs.setTabsViewRoutes(menuRule)
    // 预跳转到上次路径
    if (route.query && route.query.url && route.query.url != adminBaseRoute.path) {
        let query = JSON.parse(route.query.query as string)
        router.push({ path: route.query.url as string, query: Object.keys(query).length ? query : {} })
        return
    }
    // 跳转到第一个菜单
    let firstRoute = getFirstRoute(navTabs.state.tabsViewRoutes)
    if (firstRoute) routePush('', {}, firstRoute.path)
}

const onAdaptiveLayout = () => {
    let defaultBeforeResizeLayout = {
        layoutMode: config.layout.layoutMode,
        menuCollapse: config.layout.menuCollapse,
    }
    let beforeResizeLayout = Session.get(BEFORE_RESIZE_LAYOUT)
    if (!beforeResizeLayout) Session.set(BEFORE_RESIZE_LAYOUT, defaultBeforeResizeLayout)

    const clientWidth = document.body.clientWidth
    if (clientWidth < 1024) {
        config.setLayout('menuCollapse', true)
        config.setLayout('shrink', true)
        config.setLayoutMode('Classic')
    } else {
        let beforeResizeLayoutTemp = beforeResizeLayout || defaultBeforeResizeLayout

        config.setLayout('menuCollapse', beforeResizeLayoutTemp.menuCollapse)
        config.setLayout('shrink', false)
        config.setLayoutMode(beforeResizeLayoutTemp.layoutMode)
    }
}

// 在实例挂载后为navTabs设置一个min-width，防止宽度改变时闪现滚动条
const onSetNavTabsMinWidth = () => {
    const navTabs = document.querySelector('.nav-tabs') as HTMLElement
    if (!navTabs) {
        return
    }
    const navBar = document.querySelector('.nav-bar') as HTMLElement
    const navMenus = document.querySelector('.nav-menus') as HTMLElement
    const minWidth = navBar.offsetWidth - (navMenus.offsetWidth + 20)
    navTabs.style.width = minWidth.toString() + 'px'
}
</script>

<!-- 只有在 components 选项中的组件可以被动态组件使用-->
<script lang="ts">
export default {
    components: { Default, Classic, Streamline },
}
</script>
